//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "GDITest.h"

  extern Graphics::TBitmap *VGA = NULL;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TGDIForm *GDIForm;
//---------------------------------------------------------------------------
__fastcall TGDIForm::TGDIForm(TComponent* Owner)
  : TForm(Owner)
{
}


//----------------------//
//                      //
//----------------------//
void __fastcall TGDIForm::FormCreate(TObject *Sender)
{
  LONG Lof,
       index;
  char *p;
  BYTE vbuffer[256],
       string[8],
       buf[64*256] = { 0 },
       *buf1,
       data,
       *Srce;
  FILE *df1,
       *df2,
       *sf;

  VGA = new Graphics::TBitmap;
  //
  twidth = 0;

  if ( !VGA )
  {
    Close( );
    return;
  }

  //
/*
  VGA->Width        = 256;
  VGA->Height       = 64;
  VGA->PixelFormat  = pf1bit;
  VGA->Canvas->Font = GDIForm->Font;
*/

//  twidth = VGA->Canvas->TextWidth( "Double Moon" );
//  VGA->Canvas->TextOut( (GDIForm->ClientWidth - twidth) >> 1, 10, "Legend of Double Moon" );
//  VGA->Canvas->TextOut( 0, 10, "Double Moon" );
  VGA->LoadFromFile( "title.bmp" );
  GDIForm->Canvas->Draw( 0, 0, VGA );

  //

  df1 = fopen( "c:/romhack/trans/dm/title/title1.msk", "wb+" );
  df2 = fopen( "c:/romhack/trans/dm/title/title2.msk", "wb" );
  sf  = fopen( "c:/romhack/trans/dm/title/title.col", "rb" );
  if ( !df1 || !df2 || !sf ) return;

  fseek( sf, 0, SEEK_END );
  Srce = (BYTE *)malloc( Lof = ftell( sf ) );
  fseek( sf, 0, SEEK_SET );
  //
  if ( !Srce ){
    ShowMessage("Error: " );
    return;
  }
  //
  fread( Srce, 1, Lof, sf );

  BYTE *VRAM;
  index = 0;
  //
  for ( int y = 0; y < VGA->Height; y++ )
  {
    (void *)VRAM = VGA->ScanLine[ y ];
//    memcpy( vbuffer, VRAM, (VGA->Width >> 3) );
    memset( vbuffer, 0, 256 );
    memcpy( vbuffer, VRAM, VGA->Width );
    //
    fwrite( vbuffer, 1, 256, df1 );
//    for ( int x = 0; x < 32; x++ ){
//      Dec2Bin( string, vbuffer[ x ], 8, 2 );
//      fwrite( string, 1, 8, df1 );
//    }
  }

  fseek( df1, 0, SEEK_SET );
  fread( buf, 1, 64*256, df1 );
  fclose( df1 );

  df1 = fopen( "c:/romhack/trans/dm/title/title1.msk", "wb" );

  //-------------------//
  //                   /
  //-------------------//
  for ( int y = 0; y < 64; y++ )
  {
    for ( int x = 0; x < 128; x++ )
    { //
      if ( buf[ (y << 8) + x ] == 6 )
        fputc( 1, df1 );
      else
      if ( buf[ (y << 8) + x ] == 1 )
        fputc( 3, df1 );
      else
      if ( buf[ (y << 8) + x ] == 255 )
        fputc( 3, df1 );
      else
        fputc( 0, df1 );

      //
      if ( buf[ (y << 8) + (x + 128) ] == 6 )
        fputc( 1, df2 );
      else
      if ( buf[ (y << 8) + (x + 128) ] == 1 )
        fputc( 3, df2 );
      else
      if ( buf[ (y << 8) + (x + 128) ] == 255 )
        fputc( 3, df2 );
      else
        fputc( 0, df2 );
    }
  }
  //
  fcloseall( );
}


void __fastcall TGDIForm::FormClose(TObject *Sender, TCloseAction &Action)
{
  delete VGA;
}
//---------------------------------------------------------------------------

void __fastcall TGDIForm::FormPaint(TObject *Sender)
{
//  VGA->Canvas->TextOut( (GDIForm->ClientWidth - twidth) >> 1, 10, "Legend of Double Moon" );
//  VGA->Canvas->TextOut( 0, 10, "Double Moon" );
  GDIForm->Canvas->Draw( 0, 0, VGA );
}


//-------------------------//
//                         //
//-------------------------//
void __fastcall TGDIForm::Dec2Bin( BYTE *string, LONG valu, BYTE size, char type )
{
  char binary[256];
  int c;

  memset( binary, 0, 256 );
  //
  switch( type ){
    case 1 : for ( c = 0; c < size; c++, valu >>= 1 )
               binary[size - (c+1)] = ((valu & 0x01) | 0x01);
             //
             break; // ORA
    case 2 : for ( c = 0; c < size; c++, valu >>= 1 )
               binary[size - (c+1)] = ((valu & 0x01) ^ 0x01);
             //
             break; // EOR
    case 0 :
    default: for ( c = 0; c < size; c++, valu >>= 1 )
               binary[size - (c+1)] = (valu & 0x01);
             //  
             break; // NONE
  }
  //
  memcpy( string, binary, size );
}




